@echo off
REM Usage:
REM genModel.bat database_name
REM Example:
REM genModel.bat library
REM Move the generated files from .\genModel to the corresponding service's model directory

REM Database configuration
set host=127.0.0.1
set port=3306
set dbname=%1
set username=root
set passwd=mysql

REM Directory to generate model files
set modeldir=..\model

REM Temporary file to store table names
set tempfile=tables.tmp

REM Get all table names from the database
mysql -h %host% -P %port% -u %username% -p%passwd% -D %dbname% -e "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '%dbname%'" > %tempfile%

REM Process each table
for /f "tokens=*" %%t in (%tempfile%) do (
    if not "%%t"=="TABLE_NAME" (
        echo Starting to create table: %%t in database: %dbname%
        goctl model mysql datasource -url="%username%:%passwd%@tcp(%host%:%port%)/%dbname%" -table="%%t" -dir="%modeldir%" -cache=true --style=goZero
    )
)

REM Clean up temporary file
del %tempfile%

echo All models generated successfully.